/**
 * 创建图层图层组 入口
 */

import { createLayer } from './utils';
export const ctrlInitFun = (ctrlData, viewer, cb) => {
  for (let i = 0; i < ctrlData.length; i++) {
    const { ctrl, groupId, layerId } = ctrlData[i];
    switch (ctrl) {
      case 'createLayerGroup':
        if (groupId && !viewer.getLFTLayerGroup(groupId)) {
          viewer.addLFTLayerGroup(new DX5S.LFTLayerGroup(groupId, viewer));
        }
        break;
      case 'deleteGroup':
        if (groupId) {
          let _layerGroup = viewer.getLFTLayerGroup(groupId);
          if (_layerGroup) {
            _layerGroup.remove();
          }
        }
        break;
      case 'clearGroup':
        if (groupId) {
          let _layerGroup = viewer.getLFTLayerGroup(groupId);
          if (_layerGroup) {
            _layerGroup.clear();
          }
        }
        break;
      case 'createLayer':
        createLayer(layerId, groupId, viewer);
        break;

      case 'deleteLayer':
        if (layerId) {
          let _layer;
          if (groupId) {
            if (viewer.getLFTLayerGroup(groupId)) {
              _layer = viewer.getLFTLayerGroup(groupId).getLayer(layerId);
            }
          }
          // console.log('找到了layer', _layer)?
          if (_layer) {
            _layer.remove();
          }
        }
        break;
      case 'clearLayer':
        if (layerId) {
          let _layer;
          if (groupId) {
            if (viewer.getLFTLayerGroup(groupId)) {
              _layer = viewer.getLFTLayerGroup(groupId).getLayer(layerId);
            }
          }
          if (_layer) {
            _layer.clear();
          }
        }
        break;
      default:
        break;
    }
  }
  cb && cb();
};
